﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace YXShop.Web.admin.systeminfo
{
    public partial class thumbnails_setting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!this.Page.IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("010001011");
                InitWebControl();
                GetModel();
            }
        }
        private void GetModel()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            BLL.SystemInfo.ThumbnailsSetting bll = new YXShop.BLL.SystemInfo.ThumbnailsSetting();
            Model.SystemInfo.ThumbnailsSetting model = bll.GetModel();
            if(model!=null)
            {
                this.txtId.Value = model.Id.ToString();
                this.txtThumbnailsWidth.Text = model.ThumbnailsWidth;
                this.txtThumbnailsHeight.Text = model.ThumbnailsHeight;
                this.txtImageWidth.Text = model.ImageWidth;
                this.txtImageHeight.Text = model.ImageHeight;
                this.ckbType.SelectedValue = model.Type;
                this.txtWatermarkPicturePathImg.ImageUrl =sp.DummyPaht+ model.WatermarkPicturePath;
                this.txtWatermarkPicturePathUrl.Value = model.WatermarkPicturePath;
                this.txtCharacters.Text = model.Characters;
                this.txtImgTransparent.Text = model.ImgTransparent;
                this.txtCharTransparent.Text = model.CharTransparent.ToString();
                this.ddlWatermarkPosition.SelectedValue = model.WatermarkPosition;
            }
            model = null;
            bll = null;
        }
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtThumbnailsWidth,"生成缩略图的宽度，必须为整数","isint","必填","该项为必填项，且必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtThumbnailsHeight, "生成缩略图的高度，必须为整数", "isint", "必填", "该项为必填项，且必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtImageWidth, "生成商品图的高度，必须为整数", "isint", "必填", "该项为必填项，且必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtImageHeight, "生成商品图的高度，必须为整数", "isint", "必填", "该项为必填项，且必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtImgTransparent, "生成图片水印时，水印的透明度，该数值为0~100，0表示不透明，100表示完全透明", "isint", "必填", "该项为必填项，且必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtCharTransparent, "生成文字水印时，水印的透明度，该数值为0~100，0表示不透明，100表示完全透明", "isint", "必填", "该项为必填项，且必须为数字");
            this.txtWatermarkPicturePath.Attributes.Add("onchange", "ShowImg(this);");
            ChangeHope.WebPage.WebControl.Validate(this.txtCharacters, "选择水印为文字类型时，在此处填写水印的文字", "isnull", "必填", "该项为必填项");
            
            this.Form.Attributes.Add("onsubmit","return CheckForm()");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            YXShop.Common.PromptInfo.Popedom("010001015","对不起，您没有权限进行编辑");
            Save();
        }
        private bool UploadFile()
        {
            YXShop.Common.SysParameter sp=new YXShop.Common.SysParameter();
            ChangeHope.Common.UploadFile upload = new ChangeHope.Common.UploadFile();
            upload.PostedFile = this.txtWatermarkPicturePath;
            upload.ExtensionLim = ".gif,.jpg";
            upload.FileLengthLim =sp.ImageSize ;
            upload.SavePath = "/yxuploadfile/other";
            bool uploadSucess = upload.Upload();
            if (uploadSucess)
            {
                if (upload.HaveLoad)
                {
                    this.txtWatermarkPicturePathImg.ImageUrl = upload.FilePath;
                    this.txtWatermarkPicturePathUrl.Value = upload.FilePath;
                }
                else
                {
                    this.txtWatermarkPicturePathImg.ImageUrl = this.txtWatermarkPicturePathUrl.Value;
                }
            }
            else
            {
                this.txtWatermarkPicturePathTip.CssClass = "msgError";
            }

            return uploadSucess;

        }
        private void Save()
        {
            if (UploadFile())
            {
                YXShop.Model.SystemInfo.ThumbnailsSetting model = new YXShop.Model.SystemInfo.ThumbnailsSetting();
                YXShop.BLL.SystemInfo.ThumbnailsSetting bll = new YXShop.BLL.SystemInfo.ThumbnailsSetting();
                try 
                {
                    if (!ChangeHope.Common.ValidateHelper.IsNumber(txtThumbnailsWidth.Text.Trim()) || !ChangeHope.Common.ValidateHelper.IsNumber(txtThumbnailsHeight.Text.Trim()) || !ChangeHope.Common.ValidateHelper.IsNumber(txtImageWidth.Text.Trim()) || !ChangeHope.Common.ValidateHelper.IsNumber(txtImageHeight.Text.Trim())||!ChangeHope.Common.ValidateHelper.IsNumber(txtImgTransparent.Text.Trim())||!ChangeHope.Common.ValidateHelper.IsNumber(txtCharTransparent.Text.Trim()))
                    {
                        this.ltlMsg.Text = "设置失败！图片像素或者透明度必须是数字。";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                    if (int.Parse(txtThumbnailsWidth.Text.Trim()) <= 0 || int.Parse(txtThumbnailsHeight.Text.Trim()) <= 0 || int.Parse(txtImageWidth.Text.Trim()) <= 0 || int.Parse(txtImageHeight.Text.Trim())<=0)
                    {
                        this.ltlMsg.Text = "设置失败！图片像素必须大于0。";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                    model.ThumbnailsWidth = this.txtThumbnailsWidth.Text;
                    model.ThumbnailsHeight = this.txtThumbnailsHeight.Text;
                    model.ImageWidth = this.txtImageWidth.Text;
                    model.ImageHeight = this.txtImageHeight.Text;
                    model.Type = this.ckbType.SelectedValue;
                    model.WatermarkPicturePath = this.txtWatermarkPicturePathUrl.Value;
                    model.Characters = this.txtCharacters.Text;
                    model.ImgTransparent = this.txtImgTransparent.Text;
                    model.CharTransparent = int.Parse(this.txtCharTransparent.Text);
                    model.WatermarkPosition = this.ddlWatermarkPosition.SelectedValue;
                    model.Id = ChangeHope.Common.StringHelper.StringToInt(this.txtId.Value);
                    if (model.Id > 0)
                    {
                        bll.Update(model);
                    }
                    else
                    {
                        this.txtId.Value = bll.Add(model).ToString();
                    }
                    this.ltlMsg.Text = "设置成功！";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
                catch(Exception ex)
                {
                    this.ltlMsg.Text = "设置失败！<br>"+ex.ToString();
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                }
                finally
                {
                    ChangeHope.Common.CacheClass.RemoveOneCache("ThumbnailsSetting");
                    model = null;
                    bll = null;
                }
            }
        }
    }
}
